package cn.edu.mju.user.controller;

import cn.edu.mju.common.entity.pojo.VipInfo;
import cn.edu.mju.common.entity.result.R;
import cn.edu.mju.user.service.IVipInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Project: auth-service - VipInfoController
 * <p>Powered by 16423 On 2023-10-23 21:30:16
 *
 * @author 16423 [nanian]
 * @version 1.0
 * @since 17
 */
@RestController
@RequestMapping("/vip")
public class VipInfoController {

    private static IVipInfoService iVipInfoService;

    @Autowired
    public VipInfoController(IVipInfoService iVipInfoService) {
        VipInfoController.iVipInfoService = iVipInfoService;
    }


    //TODO openfeign调用专用
    @GetMapping("/getVipLevel")
    public int getVipLevel(@RequestParam("userId") String userId){
        VipInfo info = iVipInfoService.getOne(new LambdaQueryWrapper<VipInfo>().eq(VipInfo::getVipId, userId));
        return info.getLevelId();
    }

    //TODO 获取vip信息
    @GetMapping("/getVipInfo")
    public R<VipInfo> getVipInfo(@RequestParam("userId") String userId){
        VipInfo info = iVipInfoService.getOne(new LambdaQueryWrapper<VipInfo>().eq(VipInfo::getVipId, userId));
        return R.success(info);
    }
    //TODO 添加VIP
    @PostMapping("/addVipInfo")
    public R addVipInfo(@RequestParam("userId") String userId,@RequestParam("levelId") Integer levelId){
        VipInfo vipInfo = iVipInfoService.getOne(new LambdaQueryWrapper<VipInfo>().eq(VipInfo::getUserId, userId));
        vipInfo.setLevelId(levelId);
        boolean b = iVipInfoService.update(vipInfo,new LambdaUpdateWrapper<VipInfo>().eq(VipInfo::getVipId,vipInfo.getUserId()));
        R r = new R<>();
        if (b){
            r.setCode(200);
            r.setMsg("操作成功");
        }else {
            r.setCode(500);
            r.setMsg("操作失败");
        }
        return r;
    }

    //TODO 修改（删除）VIP 10.24
    @PostMapping("/updateVip")
    public R updateVip(@RequestParam("userId") String userId, HttpServletRequest request){

        String strLevelId = request.getParameter("levelId");
        int levelId;
        if (strLevelId == null){
            levelId = 1;
        }else {
            levelId = Integer.parseInt(strLevelId);
        }

        LambdaUpdateWrapper<VipInfo> luw = new LambdaUpdateWrapper<>();
        luw.eq(VipInfo::getUserId,userId);
        luw.set(VipInfo::getLevelId,levelId);

        boolean i = iVipInfoService.update(luw);

        R r = new R<>();
        if (i){
            r.setCode(200);
            r.setMsg("操作成功");
        }else {
            r.setCode(500);
            r.setMsg("操作失败");
        }
        return r;
    }




    public static VipInfo getVipInfoS(String userId){
        return iVipInfoService.getOne(new LambdaQueryWrapper<VipInfo>().eq(VipInfo::getVipId, userId));
    }


}